<template>
  <footer>
    <ul>
      <li
        v-for="(item, index) in tabbarList"
        :key="index"
        :class="{ active: active === index }"
        @click="tabbarClick(index, item.url)"
      >
        <span class="iconfont" :class="item.icon"></span>
        <p>{{ item.title }}</p>
      </li>
    </ul>
  </footer>
</template>

<script>
export default {
  data() {
    return {
      tabbarList: [
        {
          icon: "icon-dianying",
          title: "电影/影院",
          url: "/home/movie",
        },
        {
          icon: "icon-dianshiji",
          title: "视频",
          url: "/home/video",
        },
        {
          icon: "icon-dianying",
          title: "小视频",
          url: "/home/mini",
        },
        {
          icon: "icon-dianshiji",
          title: "演出",
          url: "/home/show",
        },
        {
          icon: "icon-dianying",
          title: "我的",
          url: "/home/mine",
        },
      ],
      active: +sessionStorage.getItem("active") || 0,
    };
  },
  methods: {
    tabbarClick(i, url) {
      // 改变样式的
      this.active = i;
      // 用到了本地存储，本地存储是不会被刷新掉的
      sessionStorage.setItem("active", this.active);
      // 切换路由
      this.$router.push(url);
    },
  },
  watch: {
    $route(to) {
      this.tabbarList.forEach((item, index) => {
        // if (to.path === item.url) {
        //   this.active = index;
        // }
        if (to.path.startsWith(item.url)) {
          this.active = index;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../assets/css/var.less";
footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 49px;
  background: #fff;

  ul {
    display: flex;
    justify-content: space-between;
    height: 100%;

    li {
      flex: 1;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      font-size: @xxs-font;

      .iconfont {
        font-size: @l-font;
      }

      p {
        margin-top: 5px;
      }
    }

    .active {
      color: @theme-color;
    }
  }
}
</style>
